package com.ldy.transmit.filter;

import java.util.Enumeration;
import java.util.NoSuchElementException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class LdyHttpServletRequest extends HttpServletRequestWrapper{
	
	/**
	 * 目标URL
	 */
	public static final String HEADER_DESTINATION_URL = "Destination-Url";
	
	private String destinationUrl = "";
	private HttpServletRequest request;
	public LdyHttpServletRequest(HttpServletRequest request) {
		super(request);
		this.destinationUrl = "";
		this.request = request;
	}
	public LdyHttpServletRequest(HttpServletRequest request, String destinitionUrl) {
		super(request);
		this.destinationUrl = destinitionUrl;
		this.request = request;
	}
	@SuppressWarnings("unchecked")
	@Override  
    public Enumeration<String> getHeaders(String name) {  
        if (null != name && name.equals(HEADER_DESTINATION_URL)) {
        	final String dest = this.destinationUrl;
            return new Enumeration<String>() {  
                private boolean hasGetted = false;  
  
                @Override  
                public String nextElement() {  
                    if (hasGetted) {  
                        throw new NoSuchElementException();  
                    } else {  
                        hasGetted = true;  
                        return dest;  
                    }  
                }  
  
                @Override  
                public boolean hasMoreElements() {  
                    return !hasGetted;  
                }  
            };  
        }  
        return super.getHeaders(name);  
    }
	@Override
	public String getHeader(String name) {
		 if (null != name && name.equals(HEADER_DESTINATION_URL)) {
			 return this.destinationUrl;
		 }
		return super.getHeader(name);
	}
	@Override
	public String getQueryString() {
		return this.request.getQueryString();
	}  
	
}
